$(function(){
	if($.cookie("collectYouleche") == null){
		$("#mainCenter").html("收藏车暂时没有数据");
	}else{
		if($.cookie("collectYouleche") != ""){
			var shoucangNum = $.cookie("collectYouleche").split('^').length;
			var vehicleIdArr = $.cookie("collectYouleche").split('^');
			var hiddenInfo = "";
			if(shoucangNum >0){
				var vehicleInfo = "";
				for(var num=1;num<=shoucangNum;num++){
					var cheId = vehicleIdArr[num - 1];
					$.ajax({
						type: "get",
						url: "che.do?action=9&id="+cheId,
						dataType: "text",
						async: false,
						success: function(v){
							hiddenInfo += v + "^";
							eval("var jsonChe = " + v);
							vehicleInfo += appendVehicle(jsonChe.imageId,jsonChe.price,jsonChe.workLength,jsonChe.maxSpeed,jsonChe.vehicleId,jsonChe.batterySize,jsonChe.electricitySize,jsonChe.A1,jsonChe.A2,jsonChe.vehicleName,jsonChe.addTime);	
						}
					});
				}
					
				$("#shoucangVehicleInfo").val(hiddenInfo);
				$("#mainCenter").html(vehicleInfo);
				
			}
		}
	}
	$(".daohangDiv").live("mouseover",function(){
		$(this).css("border-bottom","3px solid #FFF");
	});
	$(".daohangDiv").live("mouseout",function(){
		$(this).css("border-bottom","3px solid #3CAC7E");
	});
	
	$("#btnGouwuche").live("mouseover",function(){
		$(this).css("border-bottom","3px solid #FFF");
	});
	
	$("#btnGouwuche").live("mouseout",function(){
		$(this).css("border-bottom","1px solid #3CAC7E");
	});
	
	var offsetleft = document.getElementById("mainCenter").offsetLeft + 810;

	var top = $(window).scrollTop()+200; 
	var left= $(window).scrollLeft() + offsetleft + 40; 
	$("#gouwuche").css({ left:left + "px", top: top + "px" });
	$("#gouwuche").css({ left:left + "px", top: top + "px" });
	$(window).scroll(function() { 
		var top = $(window).scrollTop()+200; 
		var left= $(window).scrollLeft() + offsetleft + 40; 
		$("#gouwuche").css({ left:left + "px", top: top + "px" }); 
	}); 
});

function forwardShop(){
	window.location.href="shoucang.jsp";
}

function addShopVehicle(vehicleId){
	var idArr = $.cookie("collectYouleche");
	if(idArr == "" || idArr == null){
		$.cookie("collectYouleche", vehicleId);
	}else{
		var cookiesValue = $.cookie("collectYouleche");
		if(cookiesValue.indexOf(vehicleId) < 0){
			var num = $("#numShop").text();
			$("#numShop").text(parseInt(num) + 1);
			$("#numShopSmall").text(parseInt(num) + 1);
			var cookiesId = vehicleId + "^" + $.cookie("collectYouleche");
			$.cookie("collectYouleche", cookiesId);			
		}
		
	}
}

function search(){
	var args = "v=" + encodeURIComponent($("#txtAddress").val()) + "&h=" + encodeURIComponent($("#hidAdd").val());
	alert(args);
	$.ajax({
		type: "get",
		dataType: "json",
		url: "s.do?" + args,
		success: function(v){
			
		}
	});
}

function dateFormat(date){
	var formatDate = "";
	var month = date.split(' ')[0];
	var day = date.split(' ')[1];
	var year = date.split(' ')[2].replace(",","");
	switch(month){
		case "一月":
			month = "01";
			break;
		case "二月":
			month = "02";
			break;
		case "三月":
			month = "03";
			break;
		case "四月":
			month = "04";
			break;
		case "五月":
			month = "05";
			break;
		case "六月":
			month = "06";
			break;
		case "七月":
			month = "07";
			break;
		case "八月":
			month = "08";
			break;
		case "九月":
			month = "09";
			break;
		case "十月":
			month = "10";
			break;
		case "十一月":
			month = "11";
			break;
		case "十二月":
			month = "12";
			break;
	}
	
	day=day.replace(/,/ig,""); 
	if(day.length == 1){
		day = "0" + day;
	}
	
	formatDate = year + "-" + month + "-" + day;
	return formatDate;
}

function clearAddress(){
	$("#txtAddress").val('');
}

window.onload = function() {
	var userName = $.cookie('u0.ulezu.com');
	if(userName != null){
		$("#linkUserName").html(userName).css("background-color", "#ccc");
		$("#loginandregister").hide();
	}
};

$(window).load(function() { 
	$('.flexslider').flexslider({  
		 animation: "slide"
	});
});

function forwardHome(){
	window.location.href = "home.jsp";
}
function contactSeller(vehicleId){
	$.layer({
	    type: 2,
	    title: "联系店主（优乐车-全国最大的车辆信息交易平台）",
	    iframe: {src: '/meileche/che.do?id=' +vehicleId+'&action=4'},
	    area : ['1000px' , '500px'],
	    success : function(){
	        layer.shift('right', 400);	
	    }
	});
}

function appendVehicle(imageId,price,workLength,maxSpeed,vehicleId,batterySize,electricitySize,a1,a2,vehicleName,addTime){
	
	var info = "";
	info += "<div style=\"border:1px solid #ccc;width:800px;height:200px;float:left;margin-top: 10px;\">";

	if(imageId == null || imageId == "" || imageId == "0"){
		imageId = "nopic_200_200.jpg";
	}else{
		var extName = imageId.split(".")[1];
		var imgName = imageId.split(".")[0];
		imgName = imgName.substring(0,57);
		imageId = imgName + "200_200." + extName;
	}
	
	info += "<div style=\"float:left;width:200px;height:200px;\"><img src=\"vehicleImage/"+imageId+"\"/></div>";
	info += "<div style=\"float:left;width:500px;height:200px;\">";
	info += "<div";
	info += "	style=\"float: left; width: 385px; color: #333333; font-weight: bold; font-size: 20px; height: 50px; margin: 5px 0px 0px 15px; vertical-align: middle;\">";
	info += "	【"+vehicleName+"】"+a1+"</div>";
	info += "<div";
	info += "	style=\"float: left; width: 85px; height: 50px; margin: 5px 0px 0px 15px; color: #47B689;\">";
	info += "	[<a";
	info += "		style=\"color: #47B689; font-size: 12px; text-decoration: underline;\">查看地图</a>]";
	info += "</div>";
	info += "<div";
	info += "	style=\"text-align:right;float: left; width: 100px; height: 25px; font-weight: bold; color: #666; font-size: 14px;\">";
	info += "	店铺地址：</div>";
	info += "<div";
	info += "	style=\"float: left; width: 400px; height: 25px; color: #666;\">";
	info += "	"+a2+"</div>";
	info += "<div";
	info += "	style=\"text-align:right;float: left; width: 100px; height: 25px; font-weight: bold; color: #666; font-size: 14px;\">";
	info += "	电池：</div>";
	info += "<div";
	info += "	style=\"float: left; width: 200px; height: 25px; color: #666;\">";
	info += "	"+batterySize+"V"+electricitySize+"A</div>";
	info += "<div style=\"float: right; width: 200px; height: 85px;\">";
	info += "	<div";
	info += "		style=\"float: left; width: 70px; height: 35px; text-align: right; font-size: 14px;\">发布时间：</div>";
	info += "	<div";
	info += "		style=\"float: left; width: 130px; height: 35px; text-align: left;\">"+dateFormat(addTime)+"</div>";
	info += "	<div style=\"float: left; width: 200px; height: 50px;\">";
	info += "		<div";
	info += "			style=\"float: left; width: 50px; height: 50px; text-align: right; line-height: 50px; vertical-align: text-top; font-size: 20px; font-style: italic; color: #444;\">￥</div>";
	info += "		<div";
	info += "			style=\"float: left; width: 150px; height: 50px; text-align: left; line-height: 50px; font-size: 50px; color: #F60;\">"+price+"</div>";
			
	info += "	</div>";
		
	info += "</div>";
	info += "<div";
	info += "	style=\"text-align:right;float: left; width: 100px; height: 25px; font-weight: bold; color: #666; font-size: 14px;\">";
	info += "	蓄电行程：</div>";
	info += "<div";
	info += "	style=\"float: left; width: 200px; height: 25px; color: #666;\">";
	info += "	"+workLength+"km</div>";
	info += "<div";
	info += "	style=\"text-align:right;float: left; width: 100px; height: 25px; font-weight: bold; color: #666; font-size: 14px;\">";
	info += "	最大速度：</div>";
	info += "<div";
	info += "	style=\"float: left; width: 200px; height: 25px; color: #666;\">";
	info += "	"+maxSpeed+"km/h</div>";
	
	info += "<div style=\"float: left; width: 300px; height: 50px\">";
	info += "	<a class=\"detail-btn\" onclick=\"contactSeller('"+vehicleId+"');\">联系店主</a> <a";
	info += "		class=\"detail-btn\" href=\"/meileche/che.do?id="+vehicleId+"&action=1\" target=\"_blank\">车辆详情</a>";
	info += "</div>";
	info += "</div>";
	info += "<div style=\"width:100px;height:120px;padding-top:80px;float:left;cursor:pointer;\"><img src=\"image/remove_sc.jpg\" title=\"移除收藏车\" onclick=\"removeCar('"+vehicleId+"');\"/></div>";
	info += "</div>";
	return info;
}

function removeCar(id){
	var shoucangNum = $.cookie("collectYouleche").split('^').length;
	if(shoucangNum >0){
		var vehicleIdArr = $.cookie("collectYouleche").split('^');
		var newValue = "";
		for(var i=0;i<shoucangNum;i++){
			if(vehicleIdArr[i] != id){
				newValue += "^" + vehicleIdArr[i];
			}
		}
		newValue = newValue.substring(1, newValue.length);
		$.cookie("collectYouleche", null); 
		$.cookie("collectYouleche", newValue); 
		var shoucangVehicleInfo = $("#shoucangVehicleInfo").val();
		var shoucangVehicleInfoArr = shoucangVehicleInfo.split('^');
		var vehicleInfo = "";
		var hiddenInfo = "";
		for(var i =0; i< shoucangVehicleInfoArr.length -1; i++){
			eval("var jsonChe = " + shoucangVehicleInfoArr[i]);
			if(jsonChe.vehicleId != id){
				hiddenInfo += shoucangVehicleInfoArr[i] + "^";
				vehicleInfo += appendVehicle(jsonChe.imageId,jsonChe.price,jsonChe.workLength,jsonChe.maxSpeed,jsonChe.vehicleId,jsonChe.batterySize,jsonChe.electricitySize,jsonChe.A1,jsonChe.A2,jsonChe.vehicleName,jsonChe.addTime);	
			}
		
		}
		$("#shoucangVehicleInfo").val('');
		$("#shoucangVehicleInfo").val(hiddenInfo);
		$("#mainCenter").empty();
		$("#mainCenter").html(vehicleInfo);
		
	}
}